//
//  ST_WelcomeViewController.swift
//  BTVideo
//
//  Created by DouLei on 2019/10/9.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwiftyUserDefaults
import SnapKit
import SwifterSwift
import RxSwift
import MBProgressHUD

class ST_WelcomeViewController: UIViewController {
    
    let welcome_value:ST_WelcomeEnum
    
    init(value:ST_WelcomeEnum) {
        self.welcome_value = value
        super.init(nibName: nil, bundle: nil)
    }
    
    fileprivate lazy var play_finished = Single<Void>.create { single in
        self.welcome_view.play { _ in
            single(.success(()))
        }
        return Disposables.create()
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Color.white
        
        let topEdge = iPhoneXSeries ?  100 : 53
        
        view.addSubview(welcome_view)
        welcome_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(33)
            make.right.equalToSuperview().offset(-33)
            make.top.equalToSuperview().offset(topEdge)
            make.height.equalTo(welcome_view.snp.width)
        }
        
        binding()
        
    }
    
    private func binding(){
        
        
        switch welcome_value {
            
        case .login(let account):
            
            view.addSubview(content_label)
            content_label.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.top.equalTo(welcome_view.snp.bottom).offset(36)
            }
            
            view.addSubview(content_label2)
            content_label2.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.top.equalTo(content_label.snp.bottom).offset(15)
            }
            content_label2.text = account.userinfo?.nickname
            
            play_finished.subscribe { _ in
                
                ST_BTUserInfoManager.shared.login_ignore_i_m_message(account: account)
                
                ST_RegisterMonitor.shared.welcome(regist: false)
                
            }.disposed(by: rx.disposeBag)
            
            
        case .registry:
            
            play_finished.subscribe { _ in
                ST_RegisterMonitor.shared.welcome(regist: true)
            }.disposed(by: rx.disposeBag)
            
        }
        
        
    }
    
    lazy var welcome_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "login_welcome", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        //        control.loopAnimation = true
        
        return control
    }()
    
    
    lazy var content_label:UILabel = {
        
        let control = UILabel.init()
        control.text = "欢迎回来"
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    lazy var content_label2:UILabel = {
        
        let control = UILabel.init()
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
}


enum ST_WelcomeEnum {
    case registry
    case login(account:ST_BTAccountInfo)
}
